package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.heima.homeservicemanagementsystem.config.GithubConstants;
import com.heima.homeservicemanagementsystem.config.annotation.RestControllerMapping;
import com.heima.homeservicemanagementsystem.entity.GithubInfo;
import com.heima.homeservicemanagementsystem.entity.giteeInfo;
import com.heima.homeservicemanagementsystem.module.Result;
import com.heima.homeservicemanagementsystem.service.GithubLoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

import static cn.dev33.satoken.SaManager.log;

/**
 * @Description
 * @Author jcf
 * @Date 2025/3/9
 */
@Tag(name = "GitHub登录")
@RestControllerMapping("/auth")
public class GitHubLoginController {
    @Resource
    private GithubLoginService githubLoginService;

    /**
     * 为了获取授权
     *
     * @return com.heima.homeservicemanagementsystem.module.Result
     * @author jinchengfei
     * @create 2025/3/10
     **/

    @SaIgnore
    @GetMapping("/getGitHubCode")
    @Operation(summary = "获取github的授权")
    public Result github() {
        return githubLoginService.getGithubAuth();
    }

    @SaIgnore
    @PostMapping("/githubAccessToken")
    @Operation(summary = "获取github的gitAccessToken")
    public Result githubAccessToken(@RequestParam String code) {
        return githubLoginService.githubAccessToken(code);
    }

    @SaIgnore
    @GetMapping("/getGithubUserInfoAndLogin")
    @Operation(summary = "获取用户的信息及登录接口")
    public Result getGiteeUserInfo(@RequestParam String accessToken) throws JsonProcessingException {
        log.info("用户的accesstoken：{}", accessToken);
        GithubInfo githubUserInfo = githubLoginService.getGithubUserInfo(accessToken);

        return githubLoginService.githubLogin(githubUserInfo);
    }

}
